#运行方法 python3 -m uvicorn 本程序名:app --host '0.0.0.0' --port 8001 --reload
#访问方法 http://ip:port/search
from fastapi import FastAPI
from starlette.requests import Request
import os
from fastapi.templating import Jinja2Templates
from starlette.staticfiles import StaticFiles
from starlette.responses import RedirectResponse
from typing import List
from fastapi import FastAPI, File, UploadFile
from starlette.responses import HTMLResponse
app = FastAPI()
#location='static'
#app.mount('/static', StaticFiles(directory=location), name='static') 
search0='''
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title></title>
  <link rel="stylesheet" href="http://cn.gdchina.club:85/static/layui/css/layui.css">
  <link rel="stylesheet" type="text/css" href="/static/mystyle.css">


</head>  
<body>
<div id="lg" class="s-p-top">
<center><img hidefocus="true" id="s_lg_img_new" class="index-logo-srcnew" src="//www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" width="270" height="129" onerror="this.src='//www.baidu.com/img/flexible/logo/pc/index@2.png';this.onerror=null;" usemap="#mp">
</center>


<center>
<form class="layui-form" action="search" text-aglin:center>
  <div class="layui-form-item">
  <div class="layui-inline" >
    <label class="layui-form-label"></label>
    <div class="layui-form-item">
     <input type="text" name="q" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input-inline" style="text-align: center;background-color: #f3fdff;width: 520px;height: 40px;">
     <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">百度一下</button>
    </div>  
    </div>
    
  </div>

  </form>
  </center>

  </body>
  </html>
'''
result0='''
<html>
<body>
{}
</body>
</html>
'''
@app.get("/")
def root():
        return RedirectResponse(url='/search')

@app.get("/search")
def search(q: str = None):
    list1=['xx信息科技有限公司','xx科技有限公司','中国工商银行中关村支行','中国工商银行股份有限公司中关村支行营业','中国工商银行江西省分行','北京银行中关村支行','中国工商银行中关村营业部','xx科技有限公司上海办公室','中国工商银行中关村外汇分公司']
    if q is None:
        return HTMLResponse(content=search0)
    searchword=q
    #关键词分割
    search_list=searchword.split(' ')

    #串联循环搜索
    for item in search_list:
        #判断是否包含搜索关键词
        list1=list(filter(lambda x:item in x, list1))
    print('相近结果为', list1)
    newlist1=''
    for item in list1:
        #newlist1=newlist1+'<center><br>'+item+'</br></center>'
        newlist1=newlist1+'<div style="position: relative;left: 500px"><br>'+item+'</br></div>'
        
    #newlist1=''.join(list1)
    for item in search_list:
        newlist1=newlist1.replace(item,'<font size="3" color="red">'+item+'</font>')
    print(newlist1)
    return HTMLResponse(content=search0+result0.format(newlist1))